<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" version="-//W3C//DTD XHTML 1.1//EN" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="js/json2.js"></script>
    <script language="javascript" type="text/javascript" src="js/jsonssproxy.js"></script>
    <link href="css/samples.css" rel="stylesheet" type="text/css">
    <title>Chained services demo</title>
</head>
<body><div id="page-content">
 
    <h1>Chained services demo</h1>

    <p class="bar">
        <input id="callCreateMessage" type="button" value="CreateMessage" onclick="callCreateMessage();">
        <input id="callDisposeMessage" type="button" value="DisposeMessage" onclick="callDisposeMessage();">
    </p>
    <p>
        There are two chained services: SourceService (<a href="source.php?service-source">view source</a>) and
        DrainService (<a href="drain.php?service-source">view source</a>):
    </p>
    <p>
        SourceService exposes one method:
        <ul>
            <li>CreateMessage() - returns string, creates string message and passes it to DrainService</li>
        </ul>
    </p>
    <p>
        DrainService exposes one method:
        <ul>
            <li>DisposeMessage() - accepts string, checks if message the same, then destroys it</li>
        </ul>
    </p>
    <p>
        Exchage log:
    </p>
    <pre id="log" class="console"></pre>

    <div class="cr_footer">Released under GPLv2, (C) 2012 Serge V. Izmaylov</div>

    <script type="text/javascript">
        //<![CDATA[
        var prxsrc, prxdrn, msg;

        function callCreateMessage() {
            try {
                $("#log").append("Calling SourceService.CreateMessage():\r\n");
                msg = prxsrc.SourceService.CreateMessage();
                $("#log").append("SourceService.CreateMessage() said: " + msg + "\r\n");
                alert("SourceService.CreateMessage() said: " + msg);
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("SourceService.CreateMessage() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("SourceService.CreateMessage() fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("callCreateMessage() exception: " + x.toString() + "\r\n");
                    alert("callCreateMessage() exception: " + x.toString());
                }
            }
        }

        function callDisposeMessage() {
            try {
                $("#log").append("Calling DrainService.DisposeMessage(\"" + msg + "\"):\r\n");
                prxdrn.DrainService.DisposeMessage(msg);
                $("#log").append("DrainService.DisposeMessage(\"" + msg + "\") succeed\r\n");
                alert("DrainService.DisposeMessage(\"" + msg + "\") succeed");
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("DrainService.DisposeMessage() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("DrainService.DisposeMessage() fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("callDisposeMessage() exception: " + x.toString() + "\r\n");
                    alert("callDisposeMessage() exception: " + x.toString());
                }
            }
        }

        $(document).ready(function () {
            try {
                var serviceurl = location.href;

                srcurl = serviceurl.replace(/chained.htm$/, "source.php");
                $("#log").append("Loading service interface:\r\n");
                $("#log").append("HTTP GET to " + srcurl + "\r\n");
                prxsrc = JSONSSProxy.fromUrl(srcurl);
                $("#log").append("Got proxy object" + "\r\n");
                $("#log").append("SourceService base url is " + prxsrc.SourceService.serviceUrl + "\r\n\r\n");

                drnurl = serviceurl.replace(/chained.htm$/, "drain.php");
                $("#log").append("Loading service interface:\r\n");
                $("#log").append("HTTP GET to " + drnurl + "\r\n");
                prxdrn = JSONSSProxy.fromUrl(drnurl);
                $("#log").append("Got proxy object" + "\r\n");
                $("#log").append("DrainService base url is " + prxdrn.DrainService.serviceUrl + "\r\n\r\n");

            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("JSONSSProxy.fromUrl fault: " + x.message + "\r\n" + JSON.stringify(x.data) + "\r\n");
                    alert("JSONSSProxy.fromUrl fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("JSONSSProxy.fromUrl exception: " + x.toString() + "\r\n");
                    alert("JSONSSProxy.fromUrl exception: " + x.toString());
                }
            }
        });
        //]]>
    </script>
</div></body>
</html>
